<%

if sUsername <> "" then

	sLoginErrorMessage = GetUserDetails(sUserName, sPassword)
	
	if sLoginErrorMessage = "" then
		
		'set session variables
		session("KeyTaxUser")	= sUsername
		session("Password")		= sPassword
		
		session("sForm") = CreateFormTypes(sUsername)
		session("sMail") = CreateEmail(sUsername)
		session("sReasons") = CreateTaxReasons()
		
		Response.Redirect("../directive/directive.asp")
		
	end if
	session("LoginFailed")	= sUsername
	
else
	sLoginErrorMessage = "Please supply a username"
end if
	


'*-------------------------------------------------------------------------------------------------------------------------
'* Method:		CreateTaxReasons
'* Parameters:	none
'* Returns:     Get all "no income tax number" tax reasons
'*-------------------------------------------------------------------------------------------------------------------------

Public Function CreateTaxReasons()
	Dim dllTaxReasons
	Dim nPos
	Dim aTaxRsn(50)
	Dim aTaxCode(50)
	Dim i,j,k
	Dim sReasons
		
	set dllTaxReasons = SERVER.CreateObject("KeyTax.clsNoItRsn")
		sReasons =  dllTaxReasons.GetNoItRsn("dir_get_no_it_rsn")
		
		i = 0
		
		do while instr(sReasons,",") <> 0
		
			nPos = instr(sReasons,",")
			aTaxRsn(i) = left(sReasons,nPos-1)
			sReasons = right(sReasons,(len(sReasons)- nPos))
			
			i = i + 1
			
			nPos = instr(sReasons,",")
			aTaxCode(j) =  left(sReasons,nPos-1)
			sReasons = right(sReasons,(len(sReasons)- nPos))	
			
			j = j + 1
			
			loop
			
			aTaxCode(j) = sReasons
			aTaxRsn(j) = sReasons
			
			aTaxRsn(i)= aTaxRsn(j)
			
			'create blank option for dropdown list
			CreateTaxReasons = "<option value=0></option>"
			
			for k = 0 to (i-1)
				aTaxRsn(i)= aTaxRsn(k)
				aTaxCode(j) = aTaxCode(k)
				CreateTaxReasons = CreateTaxReasons & "<OPTION value=" & aTaxRsn(i) & ">" & aTaxCode(j) & "</OPTION>"
			next
	
	set dllTaxReasons = nothing
	  
end Function
'*-------------------------------------------------------------------------------------------------------------------------






'*-------------------------------------------------------------------------------------------------------------------------
'* Method:		CreateEmail
'* Parameters:	none
'* Returns:     Email address(es) for this user
'*-------------------------------------------------------------------------------------------------------------------------

Public Function CreateEmail(sUsername)

	Dim dllEmail
	
	Dim aMail()
	Dim i
	Dim sMail
	dim nPos
	
	if sUsername <> "" then
		
		set dllEmail = server.CreateObject("KeyTax.ClsEmail")
			CreateEmail = dllEmail.GetEmails(cstr(sUsername))
		
	end if 'sUsername <> ""
	
	set dllEmail = nothing
	
End Function
'*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





'*--------------------------------------- Get all form types that can be used ---------------------------------------------

Public Function CreateFormTypes(sUsername)
	
	Dim dllFormTypes
	
	if sUsername <> "" then
		set dllFormTypes = server.CreateObject("KeyTax.clsDropdowns")
		CreateFormTypes = dllFormTypes.GetFormTypes(cstr(sUsername))
	end if
	
	'kill the dll object
	set dllFormTypes = nothing
	
End Function

'*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------






'*-------------------------------------------------------------------------------------------------------------------------
'* Method:		GetUserDetails
'* Parameters:	sUserName - User name
'* Returns:     Get details (ie Company ID, and user ID) for this user.
'*-------------------------------------------------------------------------------------------------------------------------

Public Function GetUserDetails(sUsername, sPassword)

	dim dllUserDetails
	dim rsUserdetails
	dim dbPassword
	dim sAuthorised
	dim sMessage

	if sUsername = "" then
		GetUserDetails = "Please supply a username"
		exit function
	end if

	'Johann Marx: Removed this check for now, as blank passwords are still allowed.
	'if sPassword = "" then
	'	GetUserDetails = "Please supply a password"
	'	exit function
	'end if

	Set dllUserDetails = Server.CreateObject("KeyTax.clsUserDetails")
	Set rsUserDetails = dllUserDetails.GetUserDetailsRS(sUsername)
	
	if NOT rsUserDetails.EOF then
		
		session("nComID")		= rsUserDetails("com_id")
		session("nUserID")		= rsUserDetails("use_id")
		if not isnull(rsUserDetails("buni_code")) then session("sBuniCode")	= rsUserDetails("buni_code")
		
		dbPassword				= rsUserDetails("password")
		sAuthorised				= rsUserDetails("authorised_yn")
		
		'check the password
		if UCASE(sPassword) <> UCASE(dbPassword) then
			GetUserDetails = "The credentials that you have specified, are incorrect"
			exit function
		end if
		
		'check if the user has access to the system
		if sAuthorised = "N" then
			
			sMessage = "mailto:" & csKTEmail & "?subject=Access revoked?"
			
			GetUserDetails = "You are not authorised to use the KeyTax system.  Please contact the <a href=""" & sMessage & """>helpdesk</a> for assistance"
			exit function
		end if
		
	else
	
		GetUserDetails = "The credentials that you have specified, are incorrect"
		exit function
		
	end if 'NOT rsUserDetails.EOF

	'clean up
	set dllUserDetails = nothing
	rsUserDetails.Close
	set rsUserDetails = nothing
End Function
'*-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------





%>